package com.tianzeculture.stagebao.activity.artist;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tianzeculture.stagebao.R;
import com.tianzeculture.stagebao.base.BaseActivity;
import com.tianzeculture.stagebao.bean.WorkTag;
import com.tianzeculture.stagebao.dialog.TagDialog;
import com.tianzeculture.stagebao.network.enity.MSG;
import com.tianzeculture.stagebao.network.util.ToastUtil;
import com.tianzeculture.stagebao.util.Util;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/7/24 0024.
 * 热门标签
 */

public class HotTagActivity extends BaseActivity {
    int tagType = -1;//-1所有 0风格标签 1外貌标签 2体型标签 3魅力部位
    List<WorkTag> tag = null;//所有标签
    List<WorkTag> checkTag = null;//已选标签

    @BindView(R.id.tagItems)
    TagFlowLayout tagItems;
    @BindView(R.id.layoutCheckTag)
    LinearLayout layoutCheckTag;
    @BindView(R.id.ll_check)
    LinearLayout llCheck;
    @BindView(R.id.btnSure)
    Button btnSure;
    @BindView(R.id.tvTitle)
    TextView tvTitle;

    TagAdapter tagAdapter = null;
    TagDialog dialog;
    int count = 0;

    @Override
    public void initUI() {
        super.initUI();
        tvTitle.setText("选择标签");
        tagType = getIntent().getIntExtra("tagType", -1);
        tag = (List<WorkTag>) getIntent().getSerializableExtra("Tag");
//        checkTag = (List<WorkTag>) getIntent().getSerializableExtra("Tag");
        checkTag = (List<WorkTag>) getIntent().getSerializableExtra("checkTag");

        dialog = new TagDialog(this);
        dialog.setOnSelectClickListener(new TagDialog.OnSelectClickListener() {
            @Override
            public void onOk(String tagString) {
                WorkTag tag = new WorkTag();
                tag.setName(tagString);
                checkTag.add(tag);
                count++;
                updateCheckTag();
            }

            @Override
            public void onCancel() {
            }
        });

        if (tag == null)
            tag = new ArrayList<>();
        if (checkTag == null)
            checkTag = new ArrayList<>();

        tagAdapter = new TagAdapter(tag) {
            @Override
            public View getView(FlowLayout parent, final int position, Object o) {
                View view = LayoutInflater.from(HotTagActivity.this).inflate(R.layout.item_work_tag, null);
                final CheckBox checkBox = view.findViewById(R.id.boxWorkTag);
                checkBox.setText(tag.get(position).getName());

                checkBox.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (checkBox.isChecked()) {
                            checkTag.add(tag.get(position));
                            count++;
                        } else {
                            checkTag.remove(tag.get(position));
                            count--;
                        }
                        updateCheckTag();
                    }
                });
                return view;
            }
        };


        for (int i = 0; i < tag.size(); i++) {
            for (int j = 0; j < checkTag.size(); j++) {
                if (tag.get(i).getName().equals(checkTag.get(j).getName())) {
                    tagAdapter.setSelected(i, tag.get(i));
                    break;
                }
            }
        }
        tagItems.getSelectedList();
        tagItems.setAdapter(tagAdapter);
        tagItems.setMaxSelectCount(5);//最多选五个

        updateCheckTag();
    }

    /**
     * 刷新已选布局
     */
    private void updateCheckTag() {
        if (count > 5) {
            ToastUtil.showShort(this, "标签只能选择五个");
            checkTag.remove(checkTag.size() - 1);
            return;
        }
        layoutCheckTag.removeAllViews();
        for (int i = 0; i < checkTag.size(); i++) {
            View view = LayoutInflater.from(this).inflate(R.layout.item_tag_delete, null);
            TextView tagName = view.findViewById(R.id.tvTagName);
//            ImageButton closeBtn = view.findViewById(R.id.ibtnClose);
            tagName.setText(checkTag.get(i).getName());
            final int finalI = i;
//            closeBtn.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    checkTag.remove(finalI);
//                    updateCheckTag();
//                }
//            });
            layoutCheckTag.addView(view);
        }
        View view2 = LayoutInflater.from(this).inflate(R.layout.item_tag_custom, null);
        CheckBox boxWorkTag = view2.findViewById(R.id.boxWorkTag);
        boxWorkTag.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.show();
            }
        });
        layoutCheckTag.addView(view2);
    }


    @OnClick(R.id.btnSure)
    public void submit() {
        Intent intent = new Intent();
        intent.putExtra("tagType", tagType);
        intent.putExtra("checkTag", (Serializable) checkTag);
        setResult(Util.HOT_TAG, intent);
        finish();
    }

    @Override
    public void refreshUI(int taskId, MSG msg) {
        super.refreshUI(taskId, msg);

    }

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_hot_tag;
    }
}
